/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionbean;

import entitybean.Users;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Admin
 */
@Stateless
public class UserSessionBean implements UserSessionBeanLocal {

    private final String active = "1";
    private final String pause = "2";
    private final String delete = "3";
    @PersistenceContext(unitName = "DVDShopOnline4-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }
    /* Phan Van Duong
     * insert member
     */
    public boolean insert(String username, String password, String phone, String birthday, String sex, String address, Date joindate, String status) {
        boolean result;
        Users u = new Users();
        u.setUsername(username);
        u.setPassword(password);
        u.setPhone(phone);
        u.setBirthday(birthday);
        u.setSex(sex);
        u.setAddress(address);
        u.setJoinDate(joindate);
        u.setStatus(status.charAt(0));
        persist(u);
        result = true;
        // }
        return result;
    }
    /*Phan Van Duong
     * check exist user have status active or pause
     */
    public boolean checkExistUser(String username) {
        Query q = em.createQuery("SELECT u FROM Users u WHERE u.username = :username AND u.status=1 OR u.status=2");
        q.setParameter("username", username);
        boolean result = false;

        List list = q.getResultList();
        if (list.size() > 0) {
            result = true;
        }
        return result;
    }
    /*Phan Van Duong
     * View all member
     */
    public List viewAll() {
        Query q = em.createNamedQuery("Users.findAll");
        return q.getResultList();
    }
    /*
     * Phan Van Duong
     * search member like username
     */
    public List search(String username) {
        Query q = em.createNamedQuery("Users.findByUsername");
        q.setParameter("username", "%"+username+"%");
        return q.getResultList();
    }
    /*Phan Van Duong
     * update detail member
     * check duplicate username have status is active or pause
     *
     */

    public boolean update(int userId, String username, String password, String phone, String birthday, String sex, String address, Date joindate, String status) {
        Query q = em.createQuery("SELECT u FROM Users u WHERE u.username = :username AND u.status=1 OR u.status=2");
        q.setParameter("username", username);
        List list = q.getResultList();
        boolean result = false;
        
        if (list.size() > 0) {
            Users u1 = (Users) list.get(0);
            Users u2 = em.find(Users.class, userId);
            if ((u1.getUserId() != u2.getUserId()) && u2.getStatus().toString().equals(delete)) {
                //can update but status not must change to active or pause
                if (status.equals(active) || status.equals(pause)) {
                    result = false;
                } else {
                    u2.setUsername(username);
                    u2.setPassword(password);
                    u2.setPhone(phone);
                    u2.setBirthday(birthday);
                    u2.setSex(sex);
                    u2.setAddress(address);
                    //u2.setJoinDate(joindate);
                    u2.setStatus(status.charAt(0));
                    persist(u2);
                    result = true;
                }
            } else if (u1.getUserId() == u2.getUserId()) {
                //Two user is one, can update
                u2.setUsername(username);
                u2.setPassword(password);
                u2.setPhone(phone);
                u2.setBirthday(birthday);
                u2.setSex(sex);
                u2.setAddress(address);
                //u2.setJoinDate(joindate);
                u2.setStatus(status.charAt(0));
                persist(u2);
                result = true;
            } else {
                result = false;                
            }
        } else {
            //Username is change and not found this username in database, so can update
            Users u = em.find(Users.class, userId);
            u.setUsername(username);
            u.setPassword(password);
            u.setPhone(phone);
            u.setBirthday(birthday);
            u.setSex(sex);
            u.setAddress(address);
            //u.setJoinDate(joindate);
            u.setStatus(status.charAt(0));
            persist(u);
            result = true;
        }
        return result;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
